<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'Write history' );

if( isset( $_GET[ 'id' ] ) ) {
	$history_id = formatInput( $_GET[ 'id' ] );
    $query = "SELECT count(*) " .
    		"FROM history " .
    		"WHERE history_id = '$history_id'";
    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	if( $row[ 'count(*)' ] == 0 ) {
		$history_id = -1;
	}
}
else {
	$history_id = -1;
}

print_history_tab( 'Write' );

$query = "SELECT editor " .
		"FROM users " .
		"WHERE user_id = " . $_SESSION[ 'user_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );

if( ( isset( $_SESSION[ 'admin' ] ) && $_SESSION[ 'admin' ] ) || $row[ 'editor' ] == 1 ) {
	$query = "SELECT history_id, title " .
			"FROM history " .
			"WHERE visible = 0 AND hide = 0";
}
else {
	$query = "SELECT history_id, title " .
		"FROM history " .
		"WHERE visible = 0 AND ruler_id = " . $_SESSION[ 'ruler_id' ];
}
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$num = mysql_numrows( $result );

if( $num > 0 ) {
	print( "<div class=\"blok\">" . translate( 'Not yet published' ) . ": \r\n" );
	$i = 0;
	while ( $i < $num ) {
		if( $i > 0 ) {
			print( ", " );
		}
	    $query2 = "SELECT seen " .
    		"FROM history_seen " .
    		"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND history_id = " . mysql_result( $result, $i, "history_id" );
	    $result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		if( isset( $row2[ 'seen' ] ) && $row2[ 'seen' ] == 0 ) {
			$title = "<strong>" . mysql_result( $result, $i, "title" ) . "</strong>";
		}
		else {
			$title = mysql_result( $result, $i, "title" );
		}
		
		print( "<a href=\"write.php?id=" . mysql_result( $result, $i, "history_id" ) . "\">$title</a>" );
		$i ++;
	}
	print( "</div><br />\r\n" );
}

$anonymous = "";

if( $history_id != -1 ) {
	if( can_edit_history( $history_id ) ) {
	    $query = "SELECT * " .
	    		"FROM history " .
	    		"WHERE history_id = " . $history_id;
	    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array( $result, MYSQL_ASSOC );
		$title = $row[ 'title' ];
		$text = $row[ 'text' ];
		$visible = $row[ 'visible' ];
		$ruler_id = $row[ 'ruler_id' ];
		$hide = $row[ 'hide' ];
		$date = $row[ 'date' ];
		$turn = $row[ 'turn' ];
		
		if( $row[ 'anonymous' ] == 1 ) {
			$anonymous = " checked='checked'";
		}
	}
	else {
		$error = translate( "You are not allowed to edit this entry, this could be due to several reasons.<br />You can only edit your own stories before they are published.<br />Editors can edit all but system stories." );
	}
}
else {
	$title = "";
	if( isset( $_SESSION[ 'old_history_text' ] ) ) {
		$text = $_SESSION[ 'old_history_text' ];
		unset( $_SESSION[ 'old_history_text' ] );
	}
	else {
		$text = "";
	}
	$visible = false;
	$ruler_id = $_SESSION[ 'ruler_id' ];	
}

if( $ruler_id == $_SESSION[ 'ruler_id' ] ) {
	$author = translate( "Me" );
}
else if( $row[ 'ruler_id' ] != -1 ) {
    $query2 = "SELECT name " .
    		"FROM ruler " .
    		"WHERE ruler_id = " . $row[ 'ruler_id' ];
    $result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$author = "<a title=\"author stats\" href=\"../overview/ruler.php?name=" . $row2[ 'name' ] . "\">" . $row2[ 'name' ] . "</a>";
}
else {
	$author = translate( "World News Reporter" );
}

if( !isset( $date ) ) {
	$date = getCurrentDate();
	$turn = getCurrentTurn();
}

if( isset( $_SESSION[ 'history_write_status' ] ) ) {
	print( "\t\t\t\t<div class=\"blok\">\r\n\t\t\t\t" . $_SESSION[ 'history_write_status' ] . "\r\n\t\t\t\t</div><br />\r\n" );
	unset( $_SESSION[ 'history_write_status' ] );
}

?>
					<div class="blok">
						<form action="submit_writing.php" method="post">
							<h2><?php transprint( 'Title' ); ?></h2><br />
							<input type="text" name="title" size="80" value="<?php print( $title ); ?>" /><br />
							<input type="hidden" name="history_id" value="<?php print( $history_id ); ?>" />
<?php
print( translate( "Date" ) . ": $date<br />" ); 
//print( "Turn: $turn<br />" ); 
print( translate( "Change date" ) . ": <input type=\"textfield\" size=\"3\" name=\"days_off\" value=\"0\" /><br />" );
print( "<small>(" . translate( "If you do not want to publish this item on the given date, enter the number of days after (for before use negative numbers)" ) . ")</small><br />" );
print( "<input type=\"hidden\" name=\"date\" value=\"$date\" />" );
?>
							<small><?php transprint( 'Author' ); ?>: <?php print( $author ); ?></small><br />
							<input type="checkbox" name="anonymous" <?php print( $anonymous ); ?> /> <?php transprint( 'Anonymous' ); ?>' <small>(<?php transprint( 'Published as World News Reporter, when anonymous you receive 50% of the rating points instead of 75% when not anonymous' ); ?>)</small><br />
							<textarea name="text" cols="80" rows="20"><?php
									print( stripslashes( $text ) ); 
								?></textarea><br />
							<br />
							<input type="submit" name="save" value="<?php transprint( 'save' ); ?>" />
<?php
	// Check if current user can publish
	if( !$visible && can_publish_history() ) {
	    $query = "UPDATE history_seen " .
	    		"SET seen = 1 " .
	    		"WHERE history_id = " . $history_id . " AND ruler_id = " . $_SESSION[ 'ruler_id' ];
	    mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		print( "<br /><br />" );
		print( "<input type=\"checkbox\" name=\"hide\" /> " . translate( 'Hide this entry <small>(Because we don\'t want to publish this)</small>' ) . "<br />" );
		print( "<input type=\"submit\" name=\"publish\" value=\"" . translate( 'publish' ) . "\" />" );
	}
	else {
?>
							<br />
							<br />
							<strong><?php transprint( 'About writing' ); ?></strong><br />
							<small>
								<?php transprint( 'You can write your own stories here, which, if published everyone can read.' ); ?><br />
								<br />
								<?php transprint( 'Things to keep in mind' ); ?>:<br />
								 - <?php transprint( 'Write properly, using sentences and markup' ); ?><br />
								 - <?php transprint( 'Try to be objective' ); ?><br />
								 - <?php transprint( 'Allowed HTML' ); ?>: &lt;a&gt;, &lt;strong&gt;, &lt;h1&gt;, &lt;h2&gt;, &lt;em&gt;, &lt;img&gt;<br />
								 - <?php transprint( 'No need to use' ); ?> &lt;br /&gt;, <?php transprint( 'just put in returns for new lines' ); ?><br />
								<br />
								<?php transprint( 'Examples of things to write about' ); ?>:<br />
								 - <?php transprint( 'War declarations' ); ?><br />
								 - <?php transprint( 'Any other diplomatic announcement' ); ?><br />
								 - <?php transprint( 'Tell your story' ); ?><br />
							</small>
<?php		
	}
?>
						</form>
					</div> <!-- close blok -->
<?php
aos_footer();
?>